﻿/*
 * Created by SharpDevelop.
 * User: Masy Freddy
 * Date: 29/09/2011
 * Time: 12:23
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Diagnostics;
using System.IO;

namespace MetaVideoEditorEvolved.Helper
{
	/// <summary>
	/// Description of Tracer.
	/// </summary>
	public class Tracer
	{
		private static object syncRoot = new object();
		private static Tracer tracer;
		private TraceSource source = new TraceSource("MetaVideoEditorEvolved");
		private TracerLevel tracerLevel = TracerLevel.Error;
		private string filename = string.Empty;
		
		private Tracer()
		{
		}

		public static Tracer TracerInstance
		{
			get
			{
				if (tracer == null)
				{
					lock (syncRoot)
					{
						tracer = new Tracer();
					}
				}

				return tracer;
			}
		}
		
		public TracerLevel Level
		{
			get
			{
				return tracerLevel;
			}
			
			set
			{
				this.tracerLevel = value;
			}
		}
		
		public string FileName
		{
			get
			{
				return filename;
			}
			
			set
			{
				filename = value;
				source.Listeners.Clear();
				source.Listeners.Add(new TextWriterTraceListener(Path.Combine(ApplicationPath.Logs, filename + ".txt")));
			}
		}

		public void TraceError(System.Exception exception)
		{
			if(this.tracerLevel == TracerLevel.Error || this.tracerLevel == TracerLevel.Verbose)
			{
				this.source.TraceData(TraceEventType.Error, 1000, exception);
				System.Diagnostics.Debug.WriteLine(exception.ToString());
			}
		}
		
		public void TraceError(string message, System.Exception exception)
		{
			if(this.tracerLevel == TracerLevel.Error || this.tracerLevel == TracerLevel.Verbose)
			{
				this.source.TraceData(TraceEventType.Error, 1000, string.Format("{0} - Exception : {1}", message, exception));
				System.Diagnostics.Debug.WriteLine(string.Format("{0} - Exception : {1}", message, exception));
				
			}
		}

		public void TraceError(string message)
		{
			if(this.tracerLevel == TracerLevel.Error || this.tracerLevel == TracerLevel.Verbose)
			{
				this.source.TraceData(TraceEventType.Error, 1000, message);
				System.Diagnostics.Debug.WriteLine(message);
			}
		}

		public void TraceInformation(string message)
		{
			if(this.tracerLevel == TracerLevel.Verbose)
			{
				this.source.TraceData(TraceEventType.Information, 1001, message);
				System.Diagnostics.Debug.WriteLine(message);
			}
		}

		public void TraceSuccessOperation(string message)
		{
			if(this.tracerLevel == TracerLevel.Verbose)
			{
				this.source.TraceData(TraceEventType.Information, 1002, message);
				System.Diagnostics.Debug.WriteLine(message);
			}
		}
	}
}
